import java.awt.*;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Vector;

import javax.swing.*;

public class DoodleCanvas extends JComponent {
	protected Image im;
	protected int width, height;
	protected Graphics offScreen;
	protected Color penColor = Color.red;
	protected Color glowColor = Color.green;//= new Color(168, 242, 164);
	protected  boolean mouseButtonDown = false;
	protected int x,y;
	protected Vector<LastSquare> vectorLastSquare = new Vector<LastSquare>();
	protected Vector<Vector<LastSquare>> vectors = new Vector<Vector<LastSquare>>();
	
	//number of columns and rows - to draw grid
	protected int n_Columns=3;
	protected int n_Rows=3;
	//protected boolean grid=true;
	private int col_Step, row_Step;
	int xRectOld, yRectOld;
	//private RunScript rs = new RunScript();
	
	public DoodleCanvas(){

		setBackground(Color.white);
		
		//making a vector with vectors elements. Those vectors will save each square drawing
		// index = n_Col * row + col
		for (int i = 0; i < (n_Columns*n_Rows); i++) {
			vectors.add(makeVectorsLastSquare());
		}
		
	}
	
	public Vector<LastSquare> makeVectorsLastSquare(){
		return new Vector<LastSquare>();
	}
	
	public void update(Graphics g){
		drawGrid();
		g.drawImage(im, 0, 0, this);
	}
	
	public void paint(Graphics g){
		
		update(g);
	}
	
	public Graphics getOffScreenGraphics(){
		
		return offScreen;
	}
	
	// method called in DoodlePassword2 - clearbutton 
	public void clearCanvas(){
		offScreen.setColor(Color.white);
		offScreen.fillRect(0, 0, width, height);
		offScreen.setColor(penColor);
		vectorLastSquare.removeAllElements();
		removeElementsVectors();
	}
	
	//
	public void setBounds(int x, int y, int width, int height){
		this.width=width;
		this.height=height;
		if(width <= 0 || height <= 0 )return;
		Image newIm = createImage(width, height);
		offScreen = newIm.getGraphics();
		//clearCanvas();
		if (im != null) {
			offScreen.drawImage(im, 0, 0, this);
		}
		im = newIm;
		super.setBounds(x, y, width, height);
		clearCanvas();
		repaint();
	}

	public void glowCell(int x, int y){
		int pxi, pyi;
		 Graphics2D g2 = (Graphics2D) offScreen;

//		//erases the last rect
//		offScreen.setColor(Color.white);
//		offScreen.drawRect((xRectOld*col_Step)+1, (yRectOld*row_Step)+1, col_Step-2, row_Step-2);
//		
//		//paints the cell where the mouse is pointing (and when gets the password)
//		offScreen.setColor(glowColor);
//		offScreen.drawRect((x*col_Step)+1, (y*row_Step)+1, col_Step-2, row_Step-2);
//		//offScreen.drawRect((x*col_Step), (y*row_Step), col_Step, row_Step);
//		repaint();
//		offScreen.setColor(penColor);
		//erases the last rect
		 
		 setSteps();
		 g2.setStroke(new BasicStroke(3));
		g2.setColor(Color.white);
		g2.drawRect((xRectOld*col_Step)+1, (yRectOld*row_Step)+1, col_Step-1, row_Step-1);
		
		//paints the cell where the mouse is pointing (and when gets the password)
		g2.setColor(glowColor);
		g2.drawRect((x*col_Step)+1, (y*row_Step)+1, col_Step-1, row_Step-1);
		//g2.drawRect((x*col_Step), (y*row_Step), col_Step, row_Step);
		repaint();
		g2.setColor(penColor); 
		 g2.setStroke(new BasicStroke(1));
		 
		xRectOld=x;
		yRectOld=y;
		repaint();
	}
	
	public int getCol_Step(){
		return col_Step;
	}
	public int getRow_Step(){
		return row_Step;
	}
	
	
	private void drawGrid(){
		
		int i,j;

		setSteps();
		offScreen.setColor(Color.darkGray);	
		  offScreen.drawRect(0, 0, width - 1, height - 1);
		
			//draw columns
			for (i = 1 ; i < n_Columns; i++) {
				offScreen.drawLine(col_Step*i, 0, col_Step*i, height);
			}
			//draw lines
			for (j = 1; j< n_Rows; j++) {
				offScreen.drawLine(0, row_Step*j, width, row_Step*j);
			}
		
			offScreen.setColor(penColor);
	}
	
	public void removeElementsVectors(){	//used when clear button is pressed

		vectors.removeAllElements(); 
		for (int i = 0; i < (n_Columns*n_Rows); i++) {
			vectors.add(makeVectorsLastSquare());	//restores original empty vector,
		}
	}
	
	public void setN_Columns(int n){
		n_Columns=n;
	}
	public void setN_Rows(int n){
		n_Rows=n;
	}
	
	public void setSteps(){
		row_Step=height/n_Rows;
		col_Step=width/n_Columns;
	}
	
	
	
}